#include "Message.h"
#include "giconpointer.h"
namespace MessagingMenu
{
Message::Message(Source *parent, const QString &id, const QUrl &iconUrl) :
    QObject(parent), m_source(parent), m_msg(0),
    m_id(id), m_iconUrl(iconUrl)
{
}

Message::~Message()
{
    if (m_msg) {
        g_object_unref(m_msg);
    }
}

QString Message::id() const
{
    return m_id;
}

QUrl Message::iconUrl() const
{
    return m_iconUrl;
}

QString Message::title() const
{
    return m_title;
}

QString Message::subtitle() const
{
    return m_subtitle;
}

QString Message::body() const
{
    return m_body;
}

QDateTime Message::time() const
{
    return m_time;
}

void Message::create()
{
    GIconPointer icon(m_iconUrl);
    if (m_msg != NULL) {
        g_object_unref(m_msg);
    }
    m_msg = messaging_menu_message_new(m_id.toUtf8().data(),
                                       *icon,
                                       m_title.toUtf8().data(),
                                       m_subtitle.toUtf8().data(),
                                       m_body.toUtf8().data(),
                                       m_time.toMSecsSinceEpoch() * 1000
                                       );
    messaging_menu_app_append_message(m_source->getApp(), m_msg, m_source->id().toUtf8().data(), TRUE);

}

void Message::remove()
{
    messaging_menu_app_remove_message(m_source->getApp(), m_msg);
}

MessagingMenuMessage *Message::message()
{
    return m_msg;
}

void Message::setId(QString id)
{
    if (m_id == id)
        return;

    m_id = id;
    Q_EMIT idChanged(id);
}

void Message::setIconUrl(QUrl iconUrl)
{
    if (m_iconUrl == iconUrl)
        return;

    m_iconUrl = iconUrl;
    Q_EMIT iconUrlChanged(iconUrl);
}

void Message::setTitle(QString title)
{
    if (m_title == title)
        return;

    m_title = title;
    Q_EMIT titleChanged(title);
}

void Message::setSubtitle(QString subtitle)
{
    if (m_subtitle == subtitle)
        return;

    m_subtitle = subtitle;
    Q_EMIT subtitleChanged(subtitle);
}

void Message::setBody(QString body)
{
    if (m_body == body)
        return;

    m_body = body;
    Q_EMIT bodyChanged(body);
}

void Message::setTime(QDateTime time)
{
    if (m_time == time)
        return;

    m_time = time;
    Q_EMIT timeChanged(time);
}

}
